﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;

namespace Authentication.Core.ServiceCommand
{
    public class AddEditDaoRequestServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateEditDaoRequestRequest, ResponseRecord>
    {
        private UserRecord currentUser;
        public AddEditDaoRequestServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateEditDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            string error = string.Empty;
            EditDaoRequest editRequest = new EditDaoRequest();
            try
            {
                var oldDao = Providers.SaleCode.RetrieveDaoCodeByDao(record.Dao, currentUser);
                if (oldDao.DeptParent != record.DeptParent)
                {
                    var branch = Providers.Branch.RetrieveBranchByDeptParent(record.DeptParent);
                    if (branch != null)
                    {

                        editRequest = new EditDaoRequest
                        {
                            Pic = currentUser.UserName,
                            BranchCodeSme = branch.BranchCodeSme,
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            Dao = record.Dao,
                            LinkOfferLetter = record.PathOfferLetter,
                            Status = (int)StatusCloseDaoEnum.Request
                        };
                    }
                    else
                    {
                        response.ErrorMessage = "Không xác định được trung tâm của bạn";
                        return false;
                    }
                }
                else
                {
                    editRequest = new EditDaoRequest
                    {
                        Pic = currentUser.UserName,
                        BranchCodeSme = currentUser.BranchCode,
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now,
                        Dao = record.Dao,
                        LinkOfferLetter = record.PathOfferLetter,
                        Status = (int)StatusCloseDaoEnum.Request
                    };
                }
                editRequest = Providers.EditDao.SaveEidtDaoRequest(out error, editRequest);
                if (editRequest != null && editRequest.Id != 0)
                {
                    var requestDetail = new EditDaoRequestDetail
                    {
                        Cif = record.Cif,
                        Dao = record.Dao,
                        DaoName = record.FullName,
                        DeptLevel = "60",
                        StaffId = record.StaffId,
                        RequestId = editRequest.Id,
                        DeliveryPoint = record.PositionName,
                        Division = oldDao.Division,
                        ParentName = oldDao.Division,
                        DeptParent = record.DeptParent
                    };
                    response.IsSuccess = Providers.EditDao.SaveEidtDaoRequestDetail(requestDetail);
                }
                else
                {
                    response.ErrorMessage = error;
                    response.IsSuccess = false;
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.IsSuccess = false;
            }
            return response.IsSuccess;
        }
    }
}
